package batch.import_user;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.log4j.*;

import java.util.ArrayList;
import java.sql.*;
import java.io.*;

import batch.common.db.ConnectionManager;

public class makeTempTableExistIdnPw
{
    private final String FILE_NAME  = "/home01/sfc/batch/import_user/data/esp_200704_employees.txt";
    private final String TABLE_NAME = "G_ESP_IMPORT_TEMP";

    //arrStrData[0] : id
    //arrStrData[1] : password
    //arrStrData[2] : email
    //arrStrData[3] : firstname
    //arrStrData[4] : lastname
    //arrStrData[5] : gender
    //arrStrData[6] : birth

    public static void main(String[] args)
    {
        Logger.getLogger("import_user.makeTempTableExistIdnPw").info("( * ) Start.");

        makeTempTableExistIdnPw obj = new makeTempTableExistIdnPw();
        obj.init();

        Logger.getLogger("import_user.makeTempTableExistIdnPw").info("( * ) End.");
    }


    public void init(){
        Connection conn = null;

        BufferedReader bfrdForFile = null;

        int iRE = 0;

        ArrayList params = new ArrayList();

        String strTemp = "";
        StringBuffer sQuery = new StringBuffer()
                             .append("INSERT INTO " + TABLE_NAME + "( SEQ    \n")
                             .append("                              , USERID \n")
                             .append("                              , PASSWD \n")
                             .append("                              , FNAME  \n")
                             .append("                              , LNAME  \n")
                             .append("                              , GENDER \n")
                             .append("                              , EMAIL  \n")
                             .append("                              , BIRTH) \n")
                             .append("                        VALUES( ?      \n")
                             .append("                              , ?      \n")
                             .append("                              , ?      \n")
                             .append("                              , ?      \n")
                             .append("                              , ?      \n")
                             .append("                              , ?      \n")
                             .append("                              , ?      \n")
                             .append("                              , ?)     \n");

        Logger.getLogger("import_user.makeTempTableExistIdnPw").info(FILE_NAME);

        try{
            bfrdForFile = new BufferedReader(new InputStreamReader(new FileInputStream(FILE_NAME), "ISO-8859-1"));

            conn = ConnectionManager.getConnection();
            conn.setAutoCommit(true);

            QueryRunner qRunner = new QueryRunner();

            for(int i=0; (strTemp = bfrdForFile.readLine()) != null; i++) {
                String arrStrData[] = StringUtils.splitPreserveAllTokens(strTemp, "\t");

                for(int l=0;l<arrStrData.length;l++){
                    arrStrData[l] = StringUtils.stripToNull(arrStrData[l]);
                }

                params.clear();
                params.add(new Long(i + 1)                                 );  //SEQ
                params.add(arrStrData[0]                                   );  //USERID
                params.add(arrStrData[1]                                   );  //PASSWD
                params.add(arrStrData[3]                                   );  //FNAME
                params.add(arrStrData[4]                                   );  //LNAME
                params.add(arrStrData[5]                                   );  //GENDER
                params.add(arrStrData[2]                                   );  //EMAIL
                params.add(StringUtils.replaceChars(arrStrData[6], "-", ""));  //BIRTH

                try{
                    iRE = qRunner.update(conn, sQuery.toString(), params.toArray());
                }
                catch(SQLException ex){
                    ex.printStackTrace();
                    Logger.getLogger("import_user.makeTempTableExistIdnPw").info(ex.toString());
                }

                if(i % 500 == 0){
                    Logger.getLogger("import_user.makeTempTableExistIdnPw").info("here we are == " + i);
                }
            }
        }
        catch(Exception ex) {
            ex.printStackTrace();
            Logger.getLogger("import_user.makeTempTableExistIdnPw").info(ex.toString());
        }
        finally {
            if (bfrdForFile != null) try { bfrdForFile.close(); } catch (Exception se) {}
            if (conn != null) try { conn.close(); } catch (Exception se) {}
        }
    }
}
